package com.ljkj.zjf.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.ljkj.access.Tlog;
import com.ljkj.access.Tuser;
import com.ljkj.common.db.dao.ZjfOrderDAO;
import com.ljkj.common.db.domain.ZjfOrder;
import com.ljkj.common.db.domain.ZjfUser;
import com.ljkj.common.db.util.DateTime;
import com.ljkj.common.db.util.RandomCode;

public class ZjfOrderService {
	
	private ZjfOrderDAO zjfOrderDAO;
	private TLogService tLogService;
	private ZjfUserService zjfUserService;


	/**
	 * 获取订单
	 * @param params
	 * @param currentPage
	 * @param pageSize
	 * @param sortField
	 * @param sortOrder
	 * @return
	 */
	public String getOrdersByProperties(String params,int currentPage,int pageSize,String sortField,String sortOrder){
		
		Class[] pojos = {ZjfOrder.class};
		String[] propertyNames=null;
		Object[] values=null;
		String[] likePropertyNames = {"orderId"};
		Object[] likevalues= {params};
		String[] betweenPropertyNames=null;
		Object[][]  betweenvalues=null;
		String[] orderByPropertyNames=null;
		Object[] orderByValues=null;
		
		if(sortField != null){
			orderByPropertyNames = new String[]{sortField};
			orderByValues =  new String[]{sortOrder};
		}

		int count = this.zjfOrderDAO.getCount(pojos, propertyNames, values,likePropertyNames, likevalues, betweenPropertyNames,betweenvalues);
		this.zjfOrderDAO.setPageMaxCount(pageSize);
		List<ZjfOrder> zjfOrders = this.zjfOrderDAO.findKnowByProperties(pojos, propertyNames, values, likePropertyNames, likevalues, betweenPropertyNames, betweenvalues, orderByPropertyNames, orderByValues, currentPage);
		
		List<Object> entry_list = new ArrayList<Object>();
		//迭代遍历所有条目，放到map里
		for(ZjfOrder zjfOrder : zjfOrders){
			
			Map<String,Object> entry_map = new HashMap<String,Object>();
			entry_map.put("orderId",zjfOrder.getOrderId());
			String userId = zjfOrder.getUserId();
			entry_map.put("userId",userId);
			if(userId != null && !"".equals(userId)){
				ZjfUser zjfUser = this.zjfUserService.getZjfUserById(userId);
				if(zjfUser != null){
					entry_map.put("userNickName",zjfUser.getUserName());
					entry_map.put("userRealName",zjfUser.getUserRealName());
				}
			}
			entry_map.put("orderStatus",zjfOrder.getOrderStatus());
			entry_map.put("orderPayStatus",zjfOrder.getOrderPayStatus());
			entry_map.put("userStatus",zjfOrder.getUserStatus());
			entry_map.put("orderIsDelivery",zjfOrder.getOrderIsDelivery());
			entry_map.put("dtName",zjfOrder.getDtName());
			entry_map.put("dtNum",zjfOrder.getDtNum());
			entry_map.put("orderPayment",zjfOrder.getOrderPayment());
			entry_map.put("orderItemnum",zjfOrder.getOrderItemnum());
			entry_map.put("orderCreatetime",zjfOrder.getOrderCreatetime());
			entry_map.put("orderIp",zjfOrder.getOrderIp());
			entry_map.put("orderShipName",zjfOrder.getOrderShipName());
			entry_map.put("orderShipArea",zjfOrder.getOrderShipArea());
			entry_map.put("orderShipAddr",zjfOrder.getOrderShipAddr());
			entry_map.put("orderShipZip",zjfOrder.getOrderShipZip());
			entry_map.put("orderShipTel",zjfOrder.getOrderShipTel());
			entry_map.put("orderShipEmail",zjfOrder.getOrderShipEmail());
			entry_map.put("orderShipTime",zjfOrder.getOrderShipTime());
			entry_map.put("orderShipMobile",zjfOrder.getOrderShipMobile());
			entry_map.put("orderCostItem",zjfOrder.getOrderCostItem());
			entry_map.put("orderIsTax",zjfOrder.getOrderIsTax());
			entry_map.put("orderCostTax",zjfOrder.getOrderCostTax());
			entry_map.put("orderTaxCompany",zjfOrder.getOrderTaxCompany());
			entry_map.put("orderDiscount",zjfOrder.getOrderDiscount());
			entry_map.put("orderFinalAmount",zjfOrder.getOrderFinalAmount());
			entry_map.put("orderMemo",zjfOrder.getOrderMemo());
			entry_map.put("createTime",zjfOrder.getCreateTime());
			entry_map.put("createUser", zjfOrder.getCreateUser());
			entry_map.put("updateTime", zjfOrder.getUpdateTime());
			
			entry_map.put("updateUser", zjfOrder.getUpdateUser());
			entry_map.put("disabledState", zjfOrder.getDisabledState());
			
			entry_list.add(entry_map);
		}
		
		HashMap<String,Object> result_map = new HashMap<String, Object>();
		result_map.put("data", entry_list);
		result_map.put("total", count);
		
        return JSON.toJSONString(result_map);
	}
	
	
	
	/**
	 * 新增或更新商品
	 * @param zjfOrder
	 * @return
	 */
	public String addOrEditOrderInfo(ZjfOrder zjfOrder,Tuser tuser){
		
		Tlog tlog = new Tlog();
		
		Map<String,String> result_map = new HashMap<String, String>();
		if(zjfOrder.getOrderId() == null){
			
			
			tlog.setOpType(1);
			tlog.setContent("新增订单："+ zjfOrder.getOrderId());
			zjfOrder.setOrderId(RandomCode.makeCode());
			zjfOrder.setCreateTime(DateTime.getDateTime());
			zjfOrder.setCreateUser(tuser.getLoginName());
			if(this.saveNewOrder(zjfOrder)){
				this.tLogService.addOrEditTlogInfo(tlog, tuser);
				result_map.put("err_msg", "新增成功。");
				result_map.put("err_code", "0");
			}else{
				result_map.put("err_msg", "新增失败。");
				result_map.put("err_code", "1");
			}
			
		}else{
			try{
				
				if(zjfOrder.getDisabledState() !=null && zjfOrder.getDisabledState() == 1){
					tlog.setOpType(0);
					tlog.setContent("订单状态："+zjfOrder.getOrderId()+" 变为不可用");
				}else{
					tlog.setOpType(1);
					tlog.setContent("修改订单："+zjfOrder.getOrderId());
				}
				
				zjfOrder.setUpdateTime(DateTime.getDateTime());
				zjfOrder.setUpdateUser(tuser.getLoginName());
				this.zjfOrderDAO.update(zjfOrder);
				this.tLogService.addOrEditTlogInfo(tlog, tuser);
				
				result_map.put("err_msg", "更新成功。");
				result_map.put("err_code", "0");
			}catch(Exception ex){
				result_map.put("err_msg", "更新失败。");
				result_map.put("err_code", "1");
			}
		}
		
		return JSON.toJSONString(result_map);
	}
	
	public boolean saveNewOrder(ZjfOrder zjfOrder){
		try{
			this.zjfOrderDAO.save(zjfOrder);
			return true;
		}catch(Exception ex){
			return false;
		}
	}
	
	public ZjfOrder getOrderById(String orderId){
		return this.zjfOrderDAO.findById(orderId);
	}

	
	
	//注入DAO
	public void settLogService(TLogService tLogService) {
		this.tLogService = tLogService;
	}

	public void setZjfOrderDAO(ZjfOrderDAO zjfOrderDAO) {
		this.zjfOrderDAO = zjfOrderDAO;
	}

	public void setZjfUserService(ZjfUserService zjfUserService) {
		this.zjfUserService = zjfUserService;
	}

}
